/**
 * Basic renderer - given pos, indices, normal, tex idx, tex coord
 */
define(
["framework/graphic/WebGL", "framework/asset/AssetSyst"],
function(WebGL, AssetSyst) {
    return {
        getName: function() {
            return "SYSTEM - SPRITE BATCH";
        },

        create: function(systemMgr, shaderUrl) {
            var _sprites = [];
            var _CLASS = this;
            var _shader = AssetSyst.get(shaderUrl)
            var _ortho;
            var _vertices;
            var _indices;
            var _texCoord;

            var _self = {
                getClass: function() {
                    return _CLASS;
                },
                
                add: function(sprite) {
                    _sprites.push(sprite);
                },

                rem: function(sprite) {
                    _sprites.splice(_sprites.indexOf(sprite), 1);
                },

                init: function() {
                	var hw = WebGL.viewportWidth/2;
                	var hh = WebGL.viewportHeight/2;
                	_ortho = mat4.ortho(-hw, hw, -hh, hh, 0.01, 1000);
                	mat4.transpose(_ortho);
                },

                destroy: function() {
                    //Release shader
                    AssetSyst.let(_shader);

                    //TODO Destroy all batch
                },

                render: function() {
                    //Skip if shader has not ready
                    if(!_shader.isLoaded())
                        return;

                    //Use program
                    WebGL.gl.useProgram(_shader.program);

                    //Set camera and lights and other system-wide settings
                    WebGL.gl.uniformMatrix4fv(_shader.getUniform("uPMat"), false, _ortho);

                    //Render each batch
                    for(var i = 0; i < _sprites.length; ++i) {
                        _sprites[i].render(_shader);
                    }
                }
            };

            //Register to system
            systemMgr.addSystem(_self);

            return _self;
        }
    };
});